<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加类别</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="父级类别" prop="parentId">
        <el-input v-model="ruleForm.parentId" placeholder="清输入父类级别的id,一级类别请填0"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="关键词列表">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
      <el-form-item label="排序序号">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#999">
        </el-switch>
      </el-form-item>
        <el-form-item label="是否显示在导航栏" prop="enable">
          <el-switch
              v-model="ruleForm.nav"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        parentId: '',
        name: '',
        keywords: '',
        icon: '',
        enable: 1,
        nav: 1,
        sort: 0
      },
      rules: {
        parentId: [
          {required: true, message: '请输入品牌类别', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入品牌名称', trigger: 'blur'},
          {min: 4, max: 25, message: '长度在 4 到 25 个字符', trigger: 'blur'}
        ],
        keywords: [
          {required: true, message: '请输入品牌名称', trigger: 'blur'},
          {min: 4, max: 25, message: '长度在 4 到 25 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/categorys/add-new';
          console.log('url: ' + url);
          console.log('ruleForm:');
          console.log(this.ruleForm);

          this.axios.post(url, this.ruleForm).then((response)=> {
            console.log('服务器端响应了结果：');
            console.log(response);
            console.log(response.data);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('添加类别成功！');
              this.$message({
                message: '添加类别成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>